<?php
class MSSQL {
        private $dbuser = 'rfuserdb';
        private $dbpass = 'rfpassdb';
        
        private $dbname = 'rfmantencion';

        private $dbhost = '(LOCAL)\SQLEXPRESS';
        private $conn;

        public function __construct() {
            if (!isset($this->conn)) {
                $connectionInfo = array(
                                        
                                        "Database"=>$this->dbname);             
                $this->conn = (sqlsrv_connect($this->dbhost,$connectionInfo)) or die (sqlsrv_errors());
            }
        }
        public function getConn(){
         return $this->conn;   
        }
        public function prepare($q,$p){
         $result = sqlsrv_prepare($this->conn,$q,$p);
            if(!$result) {
             if( ($errors = sqlsrv_errors()) != null)
             {
              //echo 'MS SQL errors: ';   
              foreach( $errors as $error)
              {
               echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
               echo "code: ".$error[ 'code']."<br />";
               echo "message: ".$error[ 'message']."<br />";
               echo "<br />";
              }
             }
             exit;
            }
            return $result;   
        }
        public function query($q) {
            $result = sqlsrv_query($this->conn,$q);
            if(!$result) {
             if( ($errors = sqlsrv_errors()) != null)
             {
              //echo 'MS SQL errors: ';   
              foreach( $errors as $error)
              {
               echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
               echo "code: ".$error[ 'code']."<br />";
               echo "message: ".$error[ 'message']."<br />";
               echo "<br />";
              }
             }
             exit;
            }
            return $result;
        }

        public function fetch_array($stmt) {
            return sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
        }

        public function num_rows($stmt) {
            return sqlsrv_num_rows($stmt);
        }

        public function affected_rows($stmt) {
            return sqlsrv_rows_affected($stmt);
        }

        public function fetch_assoc($stmt) {
            return sqlsrv_fetch($stmt);
        }
    }
?>
